
const WebSocket = require('ws');

var wss = false;
function create() {
    wss = new WebSocket.Server({ port: 8100, path: '/order' });
    wss.on('connection', (ws) => {
        console.info('订单客户端已连接');
        ws.on('message', (message) => {
            const json = JSON.parse(message);
            console.info('收到订单消息:', json);
        });
        ws.on('close', () => {
            console.info('Client disconnected');
        });
    });
    console.info("订单服务器启动成功")
    return wss;
}

function send(data) {
    wss && wss.clients.forEach(function each(client) {
        if (client.readyState === WebSocket.OPEN) {
            client.send(JSON.stringify(data));
        }
    });
}

exports.send = send
exports.create = create

// create() 